/* Copyright © 2006 Sarp Arda Coskun.
* Contact e-mail: sacoskun@gmail.com

* This file is part of simpleP2P.

* simpleP2P is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License

* simpleP2P is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with AndPishti.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * Client File Part Thread
 * Client program calls these threads in order to 
 * create parts of download.

 * Author: Sarp Arda Coskun - 20201238
 * Date: 18 October 2006 with Eclipse 3.1
 * Version: 0.1 (Beta)
 * Tested on : 20 October 2006 with SuSE 10.0 Linux
 */

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class ClientFilePartThread implements Runnable
{
	private ArrayList filesSearched;
	private Socket clientToClientSocket;
	private DataOutputStream outToServer;
	private BufferedReader inFromServer;
	private int i;
	private int fileSizeToDownloadFromEachClient;
	private int remainder;
	private String fileName;
	
	public ClientFilePartThread(int i, ArrayList files, int fileSize, int remainder)
	{
		this.filesSearched = files;
		this.i = i;
		this.fileSizeToDownloadFromEachClient = fileSize;
		this.remainder = remainder;
		this.fileName = ((FileSearched)(filesSearched.get(i))).fileName;
	}
	public void run()
	{
		try
		{
			System.out.println("Part " + i + " started");
			FileSearched myFileSearched = (FileSearched)(filesSearched.get(i));

			// Sends upload request to the client.
			clientToClientSocket = new Socket(InetAddress.getByName(myFileSearched.getIpAddress()), myFileSearched.getPortNumber());
			outToServer = new DataOutputStream(clientToClientSocket.getOutputStream());
			inFromServer = new BufferedReader(new InputStreamReader(clientToClientSocket.getInputStream()));
			System.out.println("Sending send file request to the server(other client)...");
			
			// Downloads the last part plus remainder from the last client on the list.
			if(i == filesSearched.size() - 1)
			{
				outToServer.writeBytes("SEND " + myFileSearched.getFileName() + " " + i*fileSizeToDownloadFromEachClient + " " + (((i+1)*fileSizeToDownloadFromEachClient)+remainder)  + "\n");							
			}
			else
			{
				outToServer.writeBytes("SEND " + myFileSearched.getFileName() + " " + i*fileSizeToDownloadFromEachClient + " " + (i+1)*fileSizeToDownloadFromEachClient  + "\n");
			}
			
			System.out.println("Waiting for a reply from server(other client)...");						
	
			// Starts to download the file.
			inFromServer.readLine();	// FILE and fileName
			inFromServer.readLine();	// Empty line \n
			String fileInBytes = inFromServer.readLine();	// File in bytes.
			String tempFileName;
			
			tempFileName = Part.getName(fileName, i);
						
			File fileReceived = new File("shared" + File.separator + tempFileName);
			FileWriter fileWriterReceived = new FileWriter(fileReceived);

			StringTokenizer fileInBytesST = new StringTokenizer(fileInBytes);
				
			while(fileInBytesST.hasMoreTokens())
			{
				fileWriterReceived.write(Integer.parseInt(fileInBytesST.nextToken()));
			}
			
			// Closes file writer.
			fileWriterReceived.close();			

			System.out.println("Part " + i + " downloaded.");			
		}
		catch (IOException e)
		{
			System.err.print(e);
		}
	}
}

